<?php
	
class Admin_PublicidadController extends CMS_Controller_Action_Admin
{
 public function init()
    {
    	parent::init();
    }
    
 	public function indexAction()
 	{
 	  $lang = Common_Translate::getLanguage();
 	  $categories = new CMS_Models_LangPublicidadCategory();
  	  $this->view->categories = $categories->getCategoriesByLang($lang);
 	}
 	
 	public function newcategoryAction()
 	{
 		$form = $this->addCategoryForm();
 		$this->view->form = $form;	
 		$langs = new CMS_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
    
    	if (!$this->getRequest()->isPost())
     	{
       		return;
     	}
     	else
    	 {
    		if (!$form->isValid($_POST))
    	  	{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				$category = new CMS_Models_PublicidadCategory();
				$category = $category->createRow();
				$category->enabled = $values['common']['enabled'];
				$category->save();
				$langs = new CMS_Models_Lang();
				$langs = $langs->getEnabledLangs();
				foreach( $langs as $l )
				{
				  $langCategory = new CMS_Models_LangPublicidadCategory();
				  $langCategory = $langCategory->createRow();
				  $langCategory->id_category = $category->id;
				  $langCategory->lang = $l['prefix2'];
				  $langCategory->name = $values[$l['prefix2']]['name_'.$l['prefix2']];
				  $langCategory->save();
				}
				
            	$this->_redirect("/admin/publicidad");
 
			}
		}
 	}
 	
 	public function deletecategoryAction()
 	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$category = $request['category'];
    	$lc = new CMS_Models_LangPublicidadCategory();
    	$lc->delete("id_category = '$category'"); 
    	$c = new CMS_Models_PublicidadCategory();
    	$c = $c->getCategoryById($category);
    	$c->delete();
     	$this->_redirect("/admin/publicidad");
 	}
 
 	
 	private function addCategoryForm()
 	{
 	  $form = new Zend_Form();
 	  $form->setAction("/admin/publicidad/newcategory")
 			->setMethod('post');
 	  $form->setAttrib("class", "jNice");
 	  /**** Datos comunes a los idiomas ****/   	
 	  $common = new Zend_Form_SubForm();  
 	  $enabled = $common->createElement('checkbox','enabled');
 	  $enabled->setLabel('Activo:');
	  $common->addElement($enabled);	
	  $form->addSubForms(array("common" => $common));
	  
      $langs = new CMS_Models_Lang();
	  $langs = $langs->getEnabledLangs();
	  foreach( $langs as $l)
	  {
	    $sub = new Zend_Form_SubForm();
	    $name = $sub->createElement("text","name_".$l['prefix2']);
		$name->setAttrib("class", "text-long");
 	   	$name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
		$name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		$sub->addElement($name);
		$form->addSubForms(array($l['prefix2'] => $sub));
	  }
	  $form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
      return $form;
 	}
 	
 	private function editCategoryForm()
 	{
 		$form = new Zend_Form();
 		$form->setAction("/admin/publicidad/editcategory")
 				->setMethod('post');
 		$form->setAttrib("class", "jNice");		
 		$request = $this->_request->getParams();
 	    $category = $request['id_category'];
 	    $c = new CMS_Models_PublicidadCategory();
 	    $c = $c->getCategoryById($category);
 	    /***** Datos comunes a todos los idiomas ****/
 	    $common = new Zend_Form_SubForm();  
 	    $enabled = $common->createElement('checkbox','enabled');
 	    $enabled->setLabel('Activo:');
 	    $enabled->setValue($c->enabled);
	    $common->addElement($enabled);	
	    $form->addSubForms(array("common" => $common));
	    /**** Datos por idioma *****/
 	    $langs = new CMS_Models_Lang();
	    $langs = $langs->getEnabledLangs();
	    foreach( $langs as $l)
	    {
	      $sub = new Zend_Form_SubForm();
	      $langCategory = new CMS_Models_LangPublicidadCategory();
	      $langCategory = $langCategory->getCategoryByKey($c->id, $l['prefix2']);
	      $name = $sub->createElement("text","name_".$l['prefix2']);
		  $name->setAttrib("class", "text-long");
 	   	  $name->addValidator('NotEmpty')->setLabel($this->view->t->_('*Nombre:'))->setRequired(true);
		  $name->addErrorMessage($this->view->t->_('Este_campo_es_obligatorio'));
		  $name->setValue($langCategory['name']);
		  $sub->addElement($name);
		  $form->addSubForms(array($l['prefix2'] => $sub));
	    }
 	    /***************************/
 		$id = $form->createElement("hidden",'id_category');
		$id->setValue($c->id);
		$form->addElement($id);
		$form->addElement('submit', 'add', array('label' => $this->view->t->_('Aceptar')));
		return $form;
 }
 	
 	public function editcategoryAction()
	{
 		$form = $this->editCategoryForm();
 		$this->view->form = $form;	
 		$langs = new CMS_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
   	    
 		if (!$this->getRequest()->isPost())
    	{
       		return;
     	}
     	else
     	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
      	        $values = $form->getValues();
 				$category = new CMS_Models_PublicidadCategory();
				$category = $category->getCategoryById($values['id_category']);
				$category->enabled = $values['common']['enabled'];
				$category->save();
				$langs = new CMS_Models_Lang();
				$langs = $langs->getEnabledLangs();
				foreach( $langs as $l )
				{
				  $langCategory = new CMS_Models_LangPublicidadCategory();
				  $langCategory = $langCategory->getCategoryById($values['id_category'], $l['prefix2']);
				  
				  if ( count( $langCategory ) <= 0 )
				  {
                    $langCategory = new CMS_Models_LangPublicidadCategory();
				    $langCategory = $langCategory->createRow();
				  }  
				  $langCategory->id_category = $category->id;
				  $langCategory->lang = $l['prefix2'];
				  $langCategory->name = $values[$l['prefix2']]['name_'.$l['prefix2']];
				  $langCategory->save();
				}
				$this->_redirect("/admin/publicidad");
		    }
 	    }
	}    
 	
 	public function listpublicidadAction()
 	{
 		$request = $this->_request->getParams();
   		$category = $request['category']; 
    	$this->view->category = $category;
    	$lang = Common_Translate::getLanguage();
    	$c = new CMS_Models_LangPublicidadCategory();
    	$c = $c->getCategoryById($category,$lang);
   		$this->view->categoryName = $c->name;
   		$content = new CMS_Models_LangPublicidad();
 		$this->view->contents = $content->getPublicidadByCategory($category,$lang);
 	}
 	
 	public function newpublicidadAction()
 	{
	    $form = $this->addPublicidadForm();
 		$this->view->form = $form;	
        $langs = new CMS_Models_Lang();
 		$this->view->langs = $langs->getEnabledLangs();
 		if (!$this->getRequest()->isPost())
     	{
     	  	return;
     	}
     	else
    	{
    		if (!$form->isValid($_POST))
      		{
				$this->view->form = $form;
				return;
			}
			else
			{
				$values = $form->getValues();
				
				$content = new CMS_Models_Publicidad();
				$content = $content->createRow();
				$content->id_category = $values['common']['category'];
				$content->imagen= $values["common"]["imagen"];
$content->cantidad_impresiones= $values["common"]["cantidad_impresiones"];
$content->precio_total= $values["common"]["precio_total"];

				$content->save();
				$langs = new CMS_Models_Lang();
				$langs = $langs->getEnabledLangs();
				foreach( $langs as $l )
				{
				  $langPublicidad = new CMS_Models_LangPublicidad();
				  $langPublicidad = $langPublicidad->createRow();
				  $langPublicidad->id_publicidad = $content->id;
				  $langPublicidad->lang = $l['prefix2'];
                                  $langPublicidad->nombre = $values[$l["prefix2"]]["nombre_".$l["prefix2"]];

				  $langPublicidad->save();
				} 
        	   	$this->_redirect("/admin/publicidad/listpublicidad/category/".$values['common']['category']);
			}
		}
	 }
	 
    private function addPublicidadForm()
    {
        $form = new Zend_Form();
	$form->setAction("/admin/publicidad/newpublicidad")
              ->setMethod('post');
    	$form->setAttrib("class", "jNice");         
        $lang = Common_Translate::getLanguage();             
    	$id_content = $this->_request->getParam('content');
        /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$category = $form->createElement("select","category");
	$categories = new CMS_Models_LangPublicidadCategory();
	$categories = $categories->getCategoriesByLang($lang);	
	$category->setLabel('Categoría:');
	$category->addMultiOptions($this->formatCategories($categories)); 

        $imagen = $common->createElement("text", "imagen");
$imagen->setLabel("Imagen:");
$imagen->setAttrib("class","text-long");
$common->addElement($imagen);
$cantidad_impresiones = $common->createElement("text", "cantidad_impresiones");
$cantidad_impresiones->setLabel("Cantidad Impresiones:");
$cantidad_impresiones->setAttrib("class","text-long");
$cantidad_impresiones->addValidator("NotEmpty")->setLabel("Cantidad Impresiones:")->setRequired(true);
$cantidad_impresiones->addErrorMessage("Este_campo_es_obligatorio");
$common->addElement($cantidad_impresiones);
$precio_total = $common->createElement("text", "precio_total");
$precio_total->setLabel("Precio Total:");
$precio_total->setAttrib("class","text-long");
$precio_total->addValidator("NotEmpty")->setLabel("Precio Total:")->setRequired(true);
$precio_total->addErrorMessage("Este_campo_es_obligatorio");
$common->addElement($precio_total);


        $common->addElement($category);
	
	$form->addSubForms(array("common" => $common));
	/**** Fin datos comunes ***/
	/**** Datos por idioma ****/
	$langs = new CMS_Models_Lang();
	$langs = $langs->getEnabledLangs();
	foreach( $langs as $l)
	{
            $sub = new Zend_Form_SubForm();

	    $nombre = $sub->createElement("text", "nombre_".$l["prefix2"]);
$nombre->setLabel("Nombre:");
$nombre->setAttrib("class","text-long");
$nombre->addValidator("NotEmpty")->setLabel("Nombre:")->setRequired(true);
$nombre->addErrorMessage("Este_campo_es_obligatorio");
$sub->addElement($nombre);


            $form->addSubForms(array($l['prefix2'] => $sub));
	}

	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
	return $form;
    }
 	
    private function formatCategories($categories)
    {
        $options = array();
        foreach( $categories as $c )
	{
            $options[$c['id']] = $c['name'];
	}
	return $options;
    }
	
    public function editpublicidadAction()
    {
        $form = $this->editPublicidadForm();
 	$this->view->form = $form;	
   	$langs = new CMS_Models_Lang();
 	$this->view->langs = $langs->getEnabledLangs();
 	if (!$this->getRequest()->isPost())
    	{
            return;
     	}
    	else
    	{
            if (!$form->isValid($_POST))
            {
                $this->view->form = $form;
		return;
            }
            else
            {
                $values = $form->getValues();
		$content = new CMS_Models_Publicidad();
		$content = $content->getPublicidadById($values['id_publicidad']);

                $content->imagen= $values["common"]["imagen"];
$content->cantidad_impresiones= $values["common"]["cantidad_impresiones"];
$content->precio_total= $values["common"]["precio_total"];


		$content->id_category = $values['common']['category'];
		$content->save();
		$langs = new CMS_Models_Lang();
		$langs = $langs->getEnabledLangs();
		foreach( $langs as $l )
		{
                    $langPublicidad = new CMS_Models_LangPublicidad();
                    $langPublicidad = $langPublicidad->getPublicidadById($content->id, $l['prefix2']);
                    
                    if ( count( $langPublicidad) <= 0 )
                    {
                        $langPublicidad = new CMS_Models_LangPublicidad();
			$langPublicidad = $langPublicidad->createRow();
                    }
                    $langPublicidad->id_publicidad = $values['id_publicidad'];
                    $langPublicidad->lang = $l['prefix2'];

                    $langPublicidad->nombre = $values[$l["prefix2"]]["nombre_".$l["prefix2"]];


                    $langPublicidad->save();
		} 
		$this->_redirect("/admin/publicidad/listpublicidad/category/".$values['common']['category']);
            }
        }
    }
 	
    private function editPublicidadForm()
    {
        $form = new Zend_Form();
 	$form->setAction("/admin/publicidad/editpublicidad")
              ->setMethod('post');
    	$form->setAttrib("class", "jNice");   
    	$lang = Common_Translate::getLanguage();      
    	$id_publicidad = $this->_request->getParam('id_publicidad');
    
    	$publicidad = new CMS_Models_Publicidad();
    	$publicidad = $publicidad->getPublicidadById($id_publicidad);         
         /**** Datos comunes ****/
    	$common = new Zend_Form_SubForm(); 
    	$category = $form->createElement("select","category");
	$categories = new CMS_Models_LangPublicidadCategory();
	$categories = $categories->getCategoriesByLang($lang);	
	$category->setLabel('Categoría:');
	$category->addMultiOptions($this->formatCategories($categories));
	$category->setValue($publicidad->id_category); 
 	
        $imagen = $common->createElement("text", "imagen");
$imagen->setLabel("Imagen:");
$imagen->setAttrib("class","text-long");
$common->addElement($imagen);
$cantidad_impresiones = $common->createElement("text", "cantidad_impresiones");
$cantidad_impresiones->setLabel("Cantidad Impresiones:");
$cantidad_impresiones->setAttrib("class","text-long");
$cantidad_impresiones->addValidator("NotEmpty")->setLabel("Cantidad Impresiones:")->setRequired(true);
$cantidad_impresiones->addErrorMessage("Este_campo_es_obligatorio");
$common->addElement($cantidad_impresiones);
$precio_total = $common->createElement("text", "precio_total");
$precio_total->setLabel("Precio Total:");
$precio_total->setAttrib("class","text-long");
$precio_total->addValidator("NotEmpty")->setLabel("Precio Total:")->setRequired(true);
$precio_total->addErrorMessage("Este_campo_es_obligatorio");
$common->addElement($precio_total);

        $imagen->setValue($publicidad->imagen);
$cantidad_impresiones->setValue($publicidad->cantidad_impresiones);
$precio_total->setValue($publicidad->precio_total);


        $common->addElement($category);
	$form->addSubForms(array("common" => $common));
	/**** Fin datos comunes ***/     
	/**** Datos por idioma ****/
	$langs = new CMS_Models_Lang();
	$langs = $langs->getEnabledLangs();
	foreach( $langs as $l)
	{
            $sub = new Zend_Form_SubForm();
	    $langPublicidad = new CMS_Models_LangPublicidad();
	    $langPublicidad = $langPublicidad->getPublicidadByKey($publicidad->id, $l['prefix2']);
	    $name = $sub->createElement("text","title_".$l['prefix2']);
            
            $nombre = $sub->createElement("text", "nombre_".$l["prefix2"]);
$nombre->setLabel("Nombre:");
$nombre->setAttrib("class","text-long");
$nombre->addValidator("NotEmpty")->setLabel("Nombre:")->setRequired(true);
$nombre->addErrorMessage("Este_campo_es_obligatorio");
$sub->addElement($nombre);

            $nombre->setValue($langPublicidad["nombre"]);


            $form->addSubForms(array($l['prefix2'] => $sub));
	}            	
	$id = $form->createElement("hidden","id_publicidad");
	$id->setValue($publicidad->id);
    	$form->addElement($id);
     	$form->addElement('submit', 'add', array('label' => 'Aceptar'));
	return $form;
    }
 
 	public function deletepublicidadAction()
	{
 		$this->disableView();
 		$request = $this->_request->getParams();
    	$publicidad = $request['publicidad'];
   	 	$category = $request['category'];
   	 	$lc = new CMS_Models_LangPublicidad();
   	 	$lc->delete("id_publicidad = '$publicidad'");
   	 	$c = new CMS_Models_Publicidad();
   	 	$c = $c->getPublicidadById($publicidad);
    	$c->delete();
    	$this->_redirect("/admin/publicidad/listpublicidad/category/$category");
 	}
}    